package com.vivio.interview;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author ZhangyiA
 * @describe: 给定 起点 和 终点 , 寻找 起点 到 终点 的最有抵达路径
 * @date 2021/07/03
 */

class Point{
    int x , y;
    public Point(int x , int y){
        this.x = x;
        this.y = y;
    }
}

public class One_GameMapPath {

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        char[][] grid = new char[n][n];
        boolean[][] vis = new boolean[n][n];
        int sx = input.nextInt();  int sy = input.nextInt();  // 起点
        int ex = input.nextInt();  int ey = input.nextInt();  // 终点
        input.nextLine();
        // 处理输入数据
        for(int i = 0 ; i < n ; i ++){
            String str = input.nextLine();
            for(int j = 0 ; j < n ; j ++){
                grid[i][j] = str.charAt(j);
            }
        }

        // 定义四个方向
        int[] dx = {-1 , 1 , 0 , 0};
        int[] dy = {0 , 0 , -1 , 1};

        int step = 0; // 记录最短距离
        Queue<Point> que = new LinkedList<>();
        que.offer(new Point(sx , sy));
        vis[sx][sy] = true;
        while(!que.isEmpty()){
            int len = que.size();
            for(int i = 0 ; i < len ; i ++){
                Point point = que.poll();
                if(point.x == ex && point.y == ey) {
                    System.out.println(step);
                    return ;
                }
                for(int j = 0 ; j < 4 ; j ++){
                    int nx = point.x + dx[j];
                    int ny = point.y + dy[j];
                    if(nx < 0 || nx >= grid.length || ny < 0 || ny >= grid[0].length || vis[nx][ny]) continue; // 越界 或者 已经访问过
                    if(grid[nx][ny] == '#' || grid[nx][ny] == '@') continue; // 障碍物
                    que.offer(new Point(nx , ny));
                    vis[nx][ny] = true;
                }
            }
            step ++;
        }
    }
}
/*
15
0 7 7 7
*5#++B+B+++++$3
55#+++++++###$$
###$++++++#+*#+
++$@$+++$$$3+#+
+++++++++++###+
+++++###+@+++++
+++++#+++#+++++
+++++#+5+#+++++
+++++#+++#+++++
+++++@+###+++++
+###+++++++++++
+#+++++++++##++
+#++#++++++#+++
+####++++++++##
+++++++++++++#+
* */